package com.kongjs.application.controller.page;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.kongjs.application.model.entity.Posts;
import com.kongjs.application.model.entity.TermRelationships;
import com.kongjs.application.model.entity.Terms;
import com.kongjs.application.service.IPostsService;
import com.kongjs.application.service.ITermRelationshipsService;
import com.kongjs.application.service.ITermsService;
import com.kongjs.common.rest.annotation.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Page
@Controller
@RequestMapping("/tags")
public class TagPageController {
    @Resource
    private ITermsService termsService;
    @Resource
    private IPostsService postsService;
    @Resource
    private ITermRelationshipsService termRelationshipsService;

    @GetMapping
    public String index() {
        return "tags/index";
    }

    @GetMapping("/{slug}")
    public String slug(Model model, @PathVariable String slug, @RequestParam(required = false, defaultValue = "1") Long pageNum, @RequestParam(required = false, defaultValue = "10") Long pageSize) {
        model.addAttribute("slug",slug);
        Optional<Terms> optionalTerms = termsService.lambdaQuery().eq(Terms::getSlug, slug).eq(Terms::getTermGroup,1).oneOpt();
        if (optionalTerms.isPresent()) {
            Terms term = optionalTerms.get();
            Set<Long> postIds = termRelationshipsService.lambdaQuery()
                    .eq(TermRelationships::getTermTaxonomyId, term.getTermId())
                    .list().stream().map(TermRelationships::getObjectId).collect(Collectors.toSet());
            if (!CollectionUtils.isEmpty(postIds)){
                com.baomidou.mybatisplus.extension.plugins.pagination.Page<Posts> page = postsService.lambdaQuery()
                        .in(Posts::getId, postIds)
                        .page(PageDTO.of(pageNum, pageSize));
                com.kongjs.application.model.dto.PageDTO<Posts> dto = new com.kongjs.application.model.dto.PageDTO<>();
                dto.setItems(page.getRecords());
                dto.setTotal(page.getTotal());
                dto.setPages(page.getPages());
                dto.setPageNum(page.getCurrent());
                dto.setPageSize(page.getSize());
                dto.setHasPrevious(page.hasPrevious());
                dto.setHasNext(page.hasNext());
                dto.setPreviousPageNum(dto.getPageNum() - 1);
                dto.setNextPageNum(dto.getPageNum() + 1);
                model.addAttribute("postPage", dto);
            }
        }
        LambdaQueryWrapper<Terms> categoryWrapper = new LambdaQueryWrapper<>();
        categoryWrapper.eq(Terms::getTermGroup, 0);
        LambdaQueryWrapper<Terms> tagWrapper = new LambdaQueryWrapper<>();
        tagWrapper.eq(Terms::getTermGroup, 1);
        model.addAttribute("categories", termsService.list(categoryWrapper));
        model.addAttribute("tagsAll", termsService.list(tagWrapper));
        model.addAttribute("tags", termsService.list(tagWrapper));

        return "tags/index";
    }
}
